// 面向对象编程演示：从C结构体到C++类
// 编译命令：g++ -o oop_demo oop_demo.cpp

#include <bits/stdc++.h>
using namespace std;

// C语言风格的结构体
struct CStudent {
    char name[50];
    int age;
    float grade;
};

// C语言风格的函数
void printCStudent(struct CStudent s) {
    printf("姓名: %s, 年龄: %d, 成绩: %.2f\n", s.name, s.age, s.grade);
}

void updateCGrade(struct CStudent* s, float newGrade) {
    s->grade = newGrade;
}

// C++风格的类
class Student {
private:
    string name;
    int age;
    float grade;

public:
    // 构造函数
    Student(string n, int a, float g) : name(n), age(a), grade(g) {
        cout << "学生对象 " << name << " 被创建" << endl;
    }
    
    // 默认构造函数
    Student() : name(""), age(0), grade(0.0) {}
    
    // 析构函数
    ~Student() {
        cout << "学生对象 " << name << " 被销毁" << endl;
    }
    
    // 拷贝构造函数
    Student(const Student& other) {
        name = other.name;
        age = other.age;
        grade = other.grade;
        cout << "学生对象 " << name << " 被拷贝" << endl;
    }
    
    // 访问器函数
    string getName() const { return name; }
    int getAge() const { return age; }
    float getGrade() const { return grade; }
    
    // 修改器函数
    void setName(const string& n) { name = n; }
    void setAge(int a) { age = a; }
    void setGrade(float g) { grade = g; }
    
    // 成员函数
    void print() const {
        cout << "姓名: " << name << ", 年龄: " << age << ", 成绩: " << grade << endl;
    }
    
    void updateGrade(float newGrade) {
        grade = newGrade;
        cout << name << " 的成绩已更新为: " << grade << endl;
    }
    
    bool isPassing() const {
        return grade >= 60.0;
    }
    
    string getGradeLevel() const {
        if(grade >= 90) return "优秀";
        else if(grade >= 80) return "良好";
        else if(grade >= 70) return "中等";
        else if(grade >= 60) return "及格";
        else return "不及格";
    }
};

// 继承演示：研究生类
class GraduateStudent : public Student {
private:
    string researchField;
    string advisor;

public:
    GraduateStudent(string n, int a, float g, string field, string adv) 
        : Student(n, a, g), researchField(field), advisor(adv) {}
    
    string getResearchField() const { return researchField; }
    string getAdvisor() const { return advisor; }
    
    void print() const {
        Student::print(); // 调用基类方法
        cout << "研究方向: " << researchField << ", 导师: " << advisor << endl;
    }
    
    void defendThesis() {
        cout << getName() << " 正在进行 " << researchField << " 方向的论文答辩" << endl;
    }
};

// 学生管理类
class StudentManager {
private:
    vector<Student> students;

public:
    void addStudent(const Student& s) {
        students.push_back(s);
        cout << "学生 " << s.getName() << " 已添加到管理系统" << endl;
    }
    
    void printAllStudents() {
        cout << "\n=== 所有学生信息 ===" << endl;
        for(const auto& s : students) {
            s.print();
        }
    }
    
    void findStudentByName(const string& name) {
        cout << "\n=== 查找学生: " << name << " ===" << endl;
        bool found = false;
        for(const auto& s : students) {
            if(s.getName() == name) {
                s.print();
                cout << "成绩等级: " << s.getGradeLevel() << endl;
                found = true;
                break;
            }
        }
        if(!found) {
            cout << "未找到学生: " << name << endl;
        }
    }
    
    void printPassingStudents() {
        cout << "\n=== 及格学生名单 ===" << endl;
        for(const auto& s : students) {
            if(s.isPassing()) {
                s.print();
            }
        }
    }
    
    double getAverageGrade() {
        if(students.empty()) return 0.0;
        
        double sum = 0;
        for(const auto& s : students) {
            sum += s.getGrade();
        }
        return sum / students.size();
    }
};

// 函数重载演示
class Calculator {
public:
    int add(int a, int b) {
        return a + b;
    }
    
    double add(double a, double b) {
        return a + b;
    }
    
    int add(int a, int b, int c) {
        return a + b + c;
    }
    
    string add(const string& a, const string& b) {
        return a + b;
    }
};

int main() {
    cout << "面向对象编程演示" << endl;
    cout << "================" << endl;
    
    // C语言风格演示
    cout << "\n=== C语言风格演示 ===" << endl;
    struct CStudent c_student;
    strcpy(c_student.name, "张三");
    c_student.age = 20;
    c_student.grade = 85.5;
    
    printCStudent(c_student);
    updateCGrade(&c_student, 90.0);
    printCStudent(c_student);
    
    // C++风格演示
    cout << "\n=== C++风格演示 ===" << endl;
    Student student1("李四", 21, 78.5);
    Student student2("王五", 22, 92.0);
    
    student1.print();
    student1.updateGrade(88.0);
    
    cout << student1.getName() << " 是否及格: " << (student1.isPassing() ? "是" : "否") << endl;
    cout << student1.getName() << " 的成绩等级: " << student1.getGradeLevel() << endl;
    
    // 继承演示
    cout << "\n=== 继承演示 ===" << endl;
    GraduateStudent grad1("赵六", 25, 88.5, "人工智能", "张教授");
    grad1.print();
    grad1.defendThesis();
    
    // 学生管理系统演示
    cout << "\n=== 学生管理系统演示 ===" << endl;
    StudentManager manager;
    manager.addStudent(student1);
    manager.addStudent(student2);
    manager.addStudent(Student("钱七", 23, 65.0));
    manager.addStudent(Student("孙八", 24, 95.5));
    
    manager.printAllStudents();
    manager.findStudentByName("王五");
    manager.printPassingStudents();
    
    cout << "\n平均成绩: " << manager.getAverageGrade() << endl;
    
    // 函数重载演示
    cout << "\n=== 函数重载演示 ===" << endl;
    Calculator calc;
    cout << "add(3, 4) = " << calc.add(3, 4) << endl;
    cout << "add(3.5, 4.2) = " << calc.add(3.5, 4.2) << endl;
    cout << "add(1, 2, 3) = " << calc.add(1, 2, 3) << endl;
    cout << "add(\"Hello\", \"World\") = " << calc.add("Hello", "World") << endl;
    
    return 0;
}
